﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;

namespace QLTTTH
{
    public partial class frmThongTinHocVien : DevExpress.XtraEditors.XtraForm
    {
        public frmThongTinHocVien()
        {
            InitializeComponent();
        }
        #region-------------- Thiet lap cac thong tin len controls ---------------
        public void ThietLapThongTin(HocVien hv)
        {
            this.txtMaHV.Text = hv.MaHV.ToString();
            this.txtHoTen.Text = hv.HoTen;
            this.dtpNgaySinh.EditValue = hv.NgaySinh.ToShortDateString();
            this.txtDiaChi.Text = hv.DiaChi;
            this.txtEmail.Text = hv.Email;
            this.txtSoDT.Text = hv.SoDT;
            this.txtNgheNghiep.Text = hv.NgheNghiep;
            if (hv.GioiTinh.Equals("Nam"))
            {

                this.rdGioiTinh.SelectedIndex = 0;
            }
            else
                this.rdGioiTinh.SelectedIndex = 1;
        }
        #endregion

        public delegate void SendData(HocVien hv);

        public SendData sendData;
        public HocVien GetHocVien()
        {
            HocVien hv = new HocVien();
            if (!txtMaHV.Text.ToString().Equals(""))
            {
                hv.MaHV = int.Parse(txtMaHV.Text.ToString());
            }
            hv.HoTen = txtHoTen.Text;
            if (this.rdGioiTinh.SelectedIndex == 0)
            {
                hv.GioiTinh = "Nam";
            }
            else
            {
                hv.GioiTinh = "Nữ";
            }
            hv.NgaySinh = DateTime.Parse(dtpNgaySinh.Text.ToString());
            hv.DiaChi = txtDiaChi.Text;
            hv.Email = txtEmail.Text;
            hv.SoDT = txtSoDT.Text;
            hv.NgheNghiep = txtNgheNghiep.Text;

            return hv;
        }
        public void BatNutLuu()
        {
            btnLuu.Enabled = true;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (sendData != null)
            {

                var vResult = new HocVienManager().Validate(GetHocVien());
                if (vResult.Valid)
                {
                    sendData(GetHocVien());
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    XtraMessageBox.Show(vResult.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            this.Close();
        }
        public void FillControl(HocVien hv)
        {
            this.txtMaHV.Text = hv.MaHV.ToString();
            this.txtHoTen.Text = hv.HoTen;
            this.dtpNgaySinh.Text = hv.NgaySinh.ToString();
            this.txtDiaChi.Text = hv.DiaChi;
            this.txtEmail.Text = hv.Email;
            this.txtSoDT.Text = hv.SoDT;
            this.txtNgheNghiep.Text = hv.NgheNghiep;
            if (this.rdGioiTinh.SelectedIndex == 0)
            {
                hv.GioiTinh = "Nam";
            }
            else
            {
                hv.GioiTinh = "Nữ";
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            ResetControl();
            this.Close();
        }
        private void ResetControl()
        {
            this.txtMaHV.ResetText();
            this.txtHoTen.ResetText();
            this.dtpNgaySinh.ResetText();
            this.txtDiaChi.ResetText();
            this.txtEmail.ResetText();
            this.txtSoDT.ResetText();
            this.txtNgheNghiep.ResetText();
            rdGioiTinh.SelectedIndex = 0;
        }

    }
    
}